package com.hx.setting.action;

import com.hx.setting.entity.UsersEntity;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

/**
 * Generate Date : 2014-03-16
 * Generate Version : v3.0
 * Generate Name : Honey v3.1 Test
 * Generate User : THINK
 * JDK Version : 1.5
 */
@SuppressWarnings("rawtypes")
public class AdminLoginAction extends UsersAction
{

	/** session 中的key */
	public static final String	ADMIN_LOGIN_SESSION_KEY	= "ADMIN_LOGIN_USER";

	/** 
	 * 管理员登录
	 */
	public void login()
	{
		HttpServletRequest request = getRequest();
		String username = request.getParameter( "username" );
		String password = request.getParameter( "password" );
		String code = request.getParameter( "code" );
		if ( username == null || username.length() == 0 )
		{
			outMessage( 1000 ,"用户名称为空！请重新输入。" );
			return;
		}
		if ( password == null || password.length() == 0 )
		{
			outMessage( 1001 ,"密码为空！请重新输入。" );
			return;
		}
		if ( code == null || code.length() == 0 )
		{
			outMessage( 1002 ,"验证码为空！请重新输入。" );
			return;
		}
		String verifycode = (String) getSessionAttribute( "VERIFYCODE" );
		if ( verifycode == null || verifycode.length() == 0 )
		{
			outMessage( 1002 ,"验证码为空！请重新输入。" );
			return;
		}
		if ( !verifycode.equalsIgnoreCase( code ) )
		{
			outMessage( 1002 ,"验证码错误！请重新输入。" );
			return;
		}
		com.hx.setting.service.BaseService baseService = getBaseService();
		List<UsersEntity> list = baseService.selectByField( com.hx.setting.model.UsersModel.UsersTableField.USERNAME ,username );
		if ( list != null && list.size() > 0 )
		{
			UsersEntity user = list.get( 0 );
			String pwd = user.getConfirmPassword();
			if ( pwd != null && pwd.length() > 0 )
			{
				if ( pwd.equals( password ) )
				{
					setSessionAttribute( ADMIN_LOGIN_SESSION_KEY ,user );
					removeSessionAttribute( "VERIFYCODE" );
					outSuccess();
					return;
				}
				else
				{
					outMessage( 1001 ,"密码错误！请重新输入。" );
					return;
				}
			}
			else
			{
				outMessage( 1001 ,"密码为空！请重新输入。" );
				return;
			}
		}
		else
		{
			outMessage( 1000 ,"用户名称错误！请重新输入。" );
			return;
		}
	}

	/** 
	 * 登出
	 */
	public void logout()
	{
		removeSessionAttribute( ADMIN_LOGIN_SESSION_KEY );
		outSuccess();
	}

	public void update()
	{
		HttpServletRequest request = getRequest();
		String username = request.getParameter( "username" );
		String password = request.getParameter( "password" );
		String opwd = request.getParameter( "opwd" );
		if ( username == null || username.length() == 0 )
		{
			outMessage( 1000 ,"用户名称为空！请重新输入。" );
			return;
		}
		if ( password == null || password.length() == 0 )
		{
			outMessage( 1001 ,"密码为空！请重新输入。" );
			return;
		}
		if ( opwd == null || opwd.length() == 0 )
		{
			outMessage( 1002 ,"密码为空！请重新输入。" );
			return;
		}
		UsersEntity user = (UsersEntity) getSessionAttribute( ADMIN_LOGIN_SESSION_KEY );
		if ( user != null )
		{
			String pwd = user.getConfirmPassword();
			String un = user.getUserName();
			if ( un.equals( username ) && pwd.equals( opwd ) )
			{
				if ( pwd.equals( password ) )
				{
					outSuccess();
					return;
				}
				UsersEntity bean = new UsersEntity();
				bean.setId( user.getId() );
				bean.setConfirmPassword( password );
				getUsersService().updateByPrimaryKeySelective( bean );
				outSuccess();
			}
			else
			{
				outFail( "用户名或者原始密码错误." );
			}
		}
		else
		{
			Enum[] fields = new Enum[]
			{ com.hx.setting.model.UsersModel.UsersTableField.USERNAME, com.hx.setting.model.UsersModel.UsersTableField.CONFIRMPASSWORD };
			Object[] values = new Object[]
			{ username, opwd };
			List<UsersEntity> list = getUsersService().selectByFields( fields ,values );
			if ( list != null && list.size() > 0 )
			{
				user = list.get( 0 );
				String pwd = user.getConfirmPassword();
				if ( password.equals( pwd ) )
				{
					outSuccess();
					return;
				}
				UsersEntity bean = new UsersEntity();
				bean.setId( user.getId() );
				bean.setConfirmPassword( password );
				getUsersService().updateByPrimaryKeySelective( bean );
				outSuccess();
			}
			else
			{
				outFail( "用户名或者原始密码错误." );
			}
		}
	}
}
/* Honey v3.1 Test - version:1 - OriginalChecksum:B964ba41ddaae3a8ab5a94e8bc070811c (do not edit this line) */
